This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
library(arules)
library(arulesViz)
# create a list of baskets
market_basket <-
list(
c("apple", "beer", "rice", "meat"),
c("apple", "beer", "rice"),
c("apple", "beer"),
c("apple", "pear"),
c("milk", "beer", "rice", "meat"),
c("milk", "beer", "rice"),
c("milk", "beer"),
c("milk", "pear")
)
# set transaction names (T1 to T8)
#names(market_basket) <- paste("T", c(1:8), sep = "")
#trans <- as(market_basket, "transactions")
#dim(trans)
#itemLabels(trans)
#summary(trans)
#image(trans)
#itemFrequencyPlot(trans, topN=10, cex.names=1)
#Min Support 0.3, confidence as 0.5.
rules <- apriori(trans,
parameter = list(supp=0.3, conf=0.5,
maxlen=10,
target= "rules"))
Apriori
Parameter specification:
Algorithmic control:
Absolute minimum support count: 2
set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[6 item(s), 8 transaction(s)] done [0.00s].
sorting and recoding items ... [4 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 done [0.00s].
writing ... [10 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].
summary(rules)
set of 10 rules
rule length distribution (lhs + rhs):sizes
1 2
4 6
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.0 1.0 2.0 1.6 2.0 2.0
summary of quality measures:
support confidence coverage lift count
Min. :0.375 Min. :0.5000 Min. :0.5000 Min. :1.000 Min. :3.0
1st Qu.:0.375 1st Qu.:0.5000 1st Qu.:0.5625 1st Qu.:1.000 1st Qu.:3.0
Median :0.500 Median :0.5833 Median :0.7500 Median :1.000 Median :4.0
Mean :0.475 Mean :0.6417 Mean :0.7750 Mean :1.067 Mean :3.8
3rd Qu.:0.500 3rd Qu.:0.7500 3rd Qu.:1.0000 3rd Qu.:1.000 3rd Qu.:4.0
Max. :0.750 Max. :1.0000 Max. :1.0000 Max. :1.333 Max. :6.0
mining info:
inspect(rules)
plot(rules, engine = "plotly")
To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.
plot(rules, measure = "confidence")
To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.
subrules <- head(rules, n = 10, by = "confidence")
plot(subrules, method = "graph", engine = "htmlwidget")
NA
## Example 1: Create transaction data and mine association rules
a_list <- list(
c("a","b","c"),
c("a","b"),
c("a","b","d"),
c("c","e"),
c("a","b","d","e")
)
## Set transaction names
names(a_list) <- paste("Tr",c(1:5), sep = "")
a_list
$Tr1
[1] "a" "b" "c"
$Tr2
[1] "a" "b"
$Tr3
[1] "a" "b" "d"
$Tr4
[1] "c" "e"
$Tr5
[1] "a" "b" "d" "e"
## Use the constructor to create transactions
trans1 <- transactions(a_list)
trans1
transactions in sparse format with
5 transactions (rows) and
5 items (columns)
rules <- apriori(trans1)
Apriori
Parameter specification:
Algorithmic control:
Absolute minimum support count: 0
set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[5 item(s), 5 transaction(s)] done [0.00s].
sorting and recoding items ... [5 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 3 4 done [0.00s].
writing ... [19 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].
inspect(rules)
## Example 2: Mine association rules from an existing transactions dataset
## using different minimum support and minimum confidence thresholds
data("Adult")
rules <- apriori(Adult,
parameter = list(supp = 0.5, conf = 0.9, target = "rules"))
Apriori
Parameter specification:
Algorithmic control:
Absolute minimum support count: 24421
set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[115 item(s), 48842 transaction(s)] done [0.03s].
sorting and recoding items ... [9 item(s)] done [0.00s].
creating transaction tree ... done [0.02s].
checking subsets of size 1 2 3 4 done [0.00s].
writing ... [52 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].
summary(rules)
set of 52 rules
rule length distribution (lhs + rhs):sizes
1 2 3 4
2 13 24 13
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.000 2.000 3.000 2.923 3.250 4.000
summary of quality measures:
support confidence coverage lift count
Min. :0.5084 Min. :0.9031 Min. :0.5406 Min. :0.9844 Min. :24832
1st Qu.:0.5415 1st Qu.:0.9155 1st Qu.:0.5875 1st Qu.:0.9937 1st Qu.:26447
Median :0.5974 Median :0.9229 Median :0.6293 Median :0.9997 Median :29178
Mean :0.6436 Mean :0.9308 Mean :0.6915 Mean :1.0036 Mean :31433
3rd Qu.:0.7426 3rd Qu.:0.9494 3rd Qu.:0.7945 3rd Qu.:1.0057 3rd Qu.:36269
Max. :0.9533 Max. :0.9583 Max. :1.0000 Max. :1.0586 Max. :46560
mining info:
plot(rules,engine="plotly")
plot(rules, method = "graph", engine = "htmlwidget")